//
//  HomeViewController.m
//  Taotao
//
//  Created by Goofy on 9/16/16.
//  Copyright © 2016 ios01. All rights reserved.
//

#import "TTHomeViewController.h"
#import "SDCycleScrollView.h"
#import "NetWorkTool.h"
#define KWidth [UIScreen mainScreen].bounds.size.width

@interface TTHomeViewController () <SDCycleScrollViewDelegate>

@property (nonatomic,strong) NSMutableArray *imageURLStringsGroup;

@end

@implementation TTHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //组样式
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    //不允许选中
    self.tableView.allowsSelection = NO;
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
    
    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    
    self.navigationItem.title = @"";
    
    [self getCycleScrollView];
}


- (void)viewDidAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.alpha = 0;
}

//获得轮播数据
- (void) getImageURLStringsGroup{
    
    [[NetWorkTool shareManager] GET_RequestWithUrlString:@"http://manage.taotao.com/rest/api/content?page=1&rows=6&categoryId=33" parmas:nil successBloock:^(NSDictionary *responseObject) {
        
        NSArray *array = responseObject[@"rows"];
        
        [array enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [self.imageURLStringsGroup addObject:obj[@"pic"]];
            
        }];
        
        [self getCycleScrollView];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
}

//实现轮播
- (void) getCycleScrollView{
    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KWidth, 214) delegate:self placeholderImage:nil];
    
    cycleScrollView.imageURLStringsGroup = self.imageURLStringsGroup;
    
    self.tableView.tableHeaderView = cycleScrollView;
    
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    
    cycleScrollView.currentPageDotColor = [UIColor whiteColor];
}

//懒加载
-(NSMutableArray *)imageURLStringsGroup{
    if (!_imageURLStringsGroup) {
        _imageURLStringsGroup = [NSMutableArray array];
        [self getImageURLStringsGroup];
    }
    return _imageURLStringsGroup;
}

#pragma mark - SDCycleScrollView代理

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    NSLog(@"%zd",index);
    
}

#pragma mark - tableView代理方法

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%zd",indexPath.section);
}

//改变顶部状态栏颜色
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.navigationController.navigationBar.alpha = self.tableView.contentOffset.y *0.03;
    if (scrollView.contentOffset.y > 55) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    } else {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 180;
    }else if(indexPath.section == 1){
        return 200;
    }else{
        return 210;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 10;
    }
    return 5;
}

@end
